<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Nginx | VanKa</title><script src="https://cdn.bootcss.com/valine/1.4.4/Valine.min.js"></script><link rel="stylesheet" href="/vcb/css/arknights.css"><link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/highlight.js/10.1.2/styles/atom-one-dark-reasonable.min.css"><style>@font-face {
 font-family: BenderLight;
 src: local('Bender'), url("/vcb/font/BenderLight.ttf");
}
@font-face {
 font-family: 'JetBrains Mono';
 src: local('JetBrains Mono'), url('/vcb/font/JetBrainsMono-Regular.woff2') format('woff2');
}</style><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/vcb/atom.xml" title="VanKa" type="application/atom+xml">
</head><body><header><nav><a href="/vcb/">Home</a><a href="/vcb/archives/">Archives</a></nav></header><main><article><div id="post-bg"><div id="post-title"><h1>Nginx</h1><hr></div><div id="post-content"><p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/yangjisen/p/12455933.html">Nginx获取反向代理真实IP和向下转发</a></p>
<ol>
<li><p>Nginx前端的连接来自反向代理服务器，如果需要获取真实IP，可按此配置：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line">    listen       80;</span><br><span class="line">    server_name  host.domain.com;</span><br><span class="line">    charset utf-8;</span><br><span class="line">    set_real_ip_from  1.2.3.4;         //真实服务器上一级代理的IP地址或者IP段,可以写多行</span><br><span class="line">    real_ip_header    X-Forwarded-For; //告知Nginx真实客户端IP从哪个请求头获取</span><br><span class="line">    real_ip_recursive off;             //是否递归解析，off表示默认从最后一个地址开始解析</span><br><span class="line">    ...</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure></li>
<li><p>Nginx作为反向代理服务器，向下转发真实IP</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">location &#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line">//下面三句起作用</span><br><span class="line">proxy_set_header Host $host;</span><br><span class="line">proxy_set_header X-Real-IP $remote_addr;</span><br><span class="line">proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;</span><br></pre></td></tr></table></figure>
<p>更多详细的内容，请参看参考资料： </p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/mzhaox/p/11214747.html">https://www.cnblogs.com/mzhaox/p/11214747.html</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://my.oschina.net/yysue/blog/2221374">https://my.oschina.net/yysue/blog/2221374</a></p>
</li>
</ol>
<div id="paginator"></div></div><div id="post-footer"><hr><a href="/vcb/2021/08/23/RaspberryPi/">← Prev RaspberryPi</a><span style="color: #fe2"> | </span><a href="/vcb/2021/07/08/Spring%20Cloud%20Alibaba/">Spring Cloud Alibaba Next →</a><hr></div><div id="bottom-btn"><a id="to-top" href="#post-title" title="to top">∧</a></div><div id="Valine"></div><script>new Valine({
 el: '#Valine'
 , appId: ''
 , appKey: ''
 , placeholder: '此条评论委托企鹅物流发送'
})</script></div></article><aside><div id="about"><a href="/vcb/" id="logo"><img src="https://ak.hypergryph.com/assets/index/images/ak/pc/faction/1.png" alt="Logo"></a><h1 id="Dr"><a href="/"> Dr.VanKa</a></h1><section id="total"><a id="total-archives" href="/vcb/archives"><span class="total-title">Archives Total:</span><span class="total-number">12</span></a><div id="total-tags"><span class="total-title">Tags:</span><span class="total-number">13</span></div><div id="total-categories"><span class="total-title">Categories:</span><span class="total-number">1</span></div></section></div><div id="aside-block"></div><footer><nobr><span class="text-title">©</span><span class="text-content">1970 to 2020</span></nobr><wbr><nobr><span class="text-title">ICP</span><span class="text-content">——备案号——</span></nobr><wbr><wbr><nobr>published with&nbsp;<a target="_blank" rel="noopener" href="http://hexo.io">Hexo&nbsp;</a></nobr><wbr><nobr>Theme&nbsp;<a target="_blank" rel="noopener" href="https://github.com/Yue-plus/hexo-theme-arknights">Arknight&nbsp;</a></nobr><wbr><nobr>by&nbsp;<a target="_blank" rel="noopener" href="https://github.com/Yue-plus">Yue_plus</a></nobr></footer></aside></main><canvas id="canvas-dust"></canvas><script src="/vcb/js/arknights.js"></script><script src="https://cdn.bootcdn.net/ajax/libs/highlight.js/10.1.2/highlight.min.js"></script></body></html>